package com.kma.ncpractice2013.dao;

import com.kma.ncpractice2013.model.UserAccessLevel;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Illya
 */
public class UserAccessLevelDAO extends DAO<UserAccessLevel>
{
	public void create(UserAccessLevel uaLevel)
    {
        String sql = "INSERT INTO USER_ACCESS_LEVELS(DESCRIPTION) VALUES (?)";
        jdbcTemplate.update(sql, new Object[]{/*uaLevel.getId(),*/uaLevel.getDescription()});
    }

    public void update(UserAccessLevel uaLevel)
    {
        String sql = "UPDATE USER_ACCESS_LEVELS SET DESCRIPTION = ? WHERE ID = ?";
        jdbcTemplate.update(sql, new Object[]{uaLevel.getDescription(),uaLevel.getId()});
    }

    public UserAccessLevel getById(int id)
    {
        String sql = "SELECT * FROM USER_ACCESS_LEVELS WHERE ID = ?";
	    List<UserAccessLevel> uaLevelList;
	    uaLevelList = jdbcTemplate.query(sql, new UserAccessLevelRowMapper(), id);
	    return uaLevelList.get(0);
    }

    public List<UserAccessLevel> getAll()
    {
        String sql = "SELECT * FROM USER_ACCESS_LEVELS";
        List<UserAccessLevel> uaLevelList = jdbcTemplate.query(sql, new UserAccessLevelRowMapper());
        return uaLevelList;
    }

    public void deleteById(int id)
    {
        String sql = "DELETE FROM USER_ACCESS_LEVELS WHERE ID = ?";
        jdbcTemplate.update(sql,id);
    }
    
    private static final class UserAccessLevelRowMapper implements ParameterizedRowMapper<UserAccessLevel>
    {
        @Override
        public UserAccessLevel mapRow(ResultSet rs, int rowNum) throws SQLException
        {
            UserAccessLevel uaLevel = new UserAccessLevel();
            uaLevel.setId(rs.getInt("id"));
            uaLevel.setDescription(rs.getString("description"));
            return uaLevel;
        }
    }
}
